package com.clown.smsbase.Controller;

import com.clown.smsbase.Domain.BaseEntity;
import com.clown.smsbase.Domain.Result;
import com.clown.smsbase.Service.GenericManager;
import com.clown.smsbase.util.ResultUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public abstract class GenericController<T extends BaseEntity<PK>, PK extends Serializable, M extends GenericManager<T, PK>>
        extends BaseController {
    protected PK id;
    protected M manager;
    protected T model;
    protected Page<T> page;

    protected Pageable pageable;
    protected int pageNumber = 0;
    protected int pageSize = 20;

    /**
     * @param model entity model
     * @return new created entity
     */
    @PostMapping(value = "add", produces = "application/json", consumes = "application/json")
    @ResponseBody
    public Result add(@RequestBody T model) {
        this.model = model;
        Date date = new Date();
        this.model.setDateCreated(date);
        this.model.setDateModified(date);
        this.manager.save(this.model);
        return ResultUtil.success("添加成功");

    }

    /**
     * @param id entity id
     */
    @DeleteMapping(value = "delete")
    public Result delete(@RequestParam PK id) {
        this.manager.delete(id);
        return ResultUtil.success("删除成功");
    }

    @DeleteMapping(value = "deleteAll")
    public Result deleteAll(@RequestParam  PK[] ids)
    {
        this.manager.deleteAll(ids);
        return ResultUtil.success("删除成功");
    }

    @GetMapping(value = "findAll")
    public List<T> findAll()
    {
        return this.manager.findAll();
    }

    /**
     * @param id entity id
     * @return found entity
     */
    @GetMapping(value = "findById/{id}", produces = "application/json")
    @ResponseBody
    public T findById(@PathVariable PK id) {
        return this.manager.findById(id);

    }

    /**
     * 根据输入，返回分页结果中的当前页，包括当前页信息和其中的实体对象集合
     *
     * @param pageNumber number order of current page
     * @param pageSize   size of current page
     * @return pagable entity list
     */
    @GetMapping(value = "findPage")
    @ResponseBody
    public Page<T> findPage(@RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        this.pageNumber = pageNumber-1;
        this.pageSize = pageSize;
        this.pageable = PageRequest.of(this.pageNumber, this.pageSize, Sort.by(Sort.Direction.ASC, "id"));
        this.page = this.manager.findAll(this.pageable);
        return this.page;
    }

    @GetMapping(value = "findPageByName")
    @ResponseBody
    public Page<T> findPageByName(@RequestParam String name, @RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        this.pageNumber = pageNumber-1;
        this.pageSize = pageSize;
        this.pageable = PageRequest.of(this.pageNumber, this.pageSize, Sort.by(Sort.Direction.ASC, "id"));
        this.page = this.manager.findByName(name,this.pageable);
        return this.page;
    }

    /**
     * @param id    entity code to update
     * @param model entity information to update
     * @return updated entity
     */
    @PutMapping(value = "update", produces = "application/json", consumes = "application/json")
    @ResponseBody
    public Result update(@RequestBody T model) {
        model.setDateModified(new Date());// 更新修改时间
        this.manager.save(model);
        return ResultUtil.success("更新成功");
    }

}
